package com.ejoysoft.wlanyz.business; 

import com.ejoysoft.common.DbConnect;
import com.ejoysoft.common.Format;
import com.ejoysoft.common.Globa;
import com.ejoysoft.common.UID;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


public class FrePoint {

    private Globa globa;
    private DbConnect db;

    //构造方法
    public FrePoint() {
    }

    public FrePoint(Globa globa) {
        this.globa = globa;
        db = globa.db;
    }

    //构造方法 
    public FrePoint(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }
   
   /**
    * 频点备份
    * @param deviceId
    * @return
    */
    public boolean backUp(String deviceId){
    	boolean result = false;
    	String sql = "select channel from "+strTableName2+" where device_id='"+deviceId+"'";
    	ResultSet re = db.executeQuery(sql);
    	try {
    		if(re !=null && re.next())
    		{
				String channel = re.getString("channel");
				String sql2 = "update "+strTableName+" set frequentCyPoint='"+channel+"' where deviceId='"+deviceId+"'";
				if(db.executeUpdate(sql2) <=0)
				{
					DbConnect db1 = new DbConnect();
					String sql3 = "insert into "+strTableName+"(strId,deviceId,frequentCyPoint,strCreator,dtCreatTime) values(?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))";
					db1.prepareStatement(sql3);									 
					db1.setString(1, UID.getID());
					db1.setString(2, deviceId);
					db1.setString(3, channel);
					db1.setString(4, globa.loginName);
					db1.setString(5, Format.getDateTime());
					if(db1.executeUpdate()>0){
			            Globa.logger0("频点备份成功", globa.loginName, globa.loginIp, sql3, "频点备份", globa.unitCode);
					}else {
						
					}
					db1.closeCon();
					result= true;
				}
				else {
		            Globa.logger0("频点备份成功", globa.loginName, globa.loginIp, sql2, "频点备份", globa.unitCode);
					result = true;
				}
    		}
    	} catch (SQLException e) {
            Globa.logger0("频点备份异常", globa.loginName, globa.loginIp, "", "频点备份", globa.unitCode);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return result;
    }
    /**
     * 频点还原
     * @param deviceId
     * @return
     */
     public boolean restore(String deviceId){
     	boolean result = false;
     	String sql = "select frequentCyPoint from "+strTableName+" where deviceId='"+deviceId+"'";
     	ResultSet re = db.executeQuery(sql);
     	try {
 			if(re !=null && re.next())
 			{
     		
 				String channel = re.getString("frequentCyPoint");
 				String sql2 = "update "+strTableName2+" set channel='"+channel+"' where device_id='"+deviceId+"'";
 				if(db.executeUpdate(sql2) > 0)
 				{
 					Globa.logger0("频点还原成功", globa.loginName, globa.loginIp, sql2, "频点还原", globa.unitCode);
 				}
				result = true;
 			}
     	} catch (SQLException e) {
            Globa.logger0("频点还原异常", globa.loginName, globa.loginIp, "", "频点还原", globa.unitCode);
			// TODO Auto-generated catch block
			e.printStackTrace();
 		}
     	return result;
     }
     /**
      * 根据二级热点名称还原频点
      * @param acName
      * @return
      */
	public boolean restoreByAcName(String acName){
    	boolean result = false;
    	Ap objAp = new Ap(globa);
    	Vector<String> vector = objAp.listDeviceId(" where hotspot='"+acName+"'"); 
    	if(vector !=null)
    	{
    		for (int i = 0; i < vector.size(); i++) {
				String deviceid = vector.get(i);
				result = this.restore(deviceid);
			}
    	}
    	db.closeCon();
    	return result;
    }
    
    /**
     * 根据二级热点名称备份频点
     * @param acName
     * @return
     */
	public boolean backUpByAcName(String acName){
    	boolean result = false;
    	Ap objAp = new Ap(globa);
    	Vector<String> vector = objAp.listDeviceId(" where hotspot='"+acName+"'"); 
    	System.err.println(vector.size()+"=================");
    	if(vector !=null)
    	{
    		for (int i = 0; i < vector.size(); i++) {
    			String deviceid = vector.get(i); 
				System.err.println(deviceid+":频点还原至获取deviceId");
				result = this.backUp(deviceid);
			}
    	}
    	db.closeCon();
    	return result;
    }
    private String strTableName = "ap_frequencyponit_copy";
    private String strTableName2 = "nhm_channel_change"; 
    private String strId;
    private String deviceId;
    private String frequentCopyPoint;
    private String strCreator;
    private String dtCreatTime;
    private String device_id;
    private String channel;

	public Globa getGloba() {
		return globa;
	}

	public void setGloba(Globa globa) {
		this.globa = globa;
	}

	public DbConnect getDb() {
		return db;
	}

	public void setDb(DbConnect db) {
		this.db = db;
	}

	public String getStrTableName() {
		return strTableName;
	}

	public void setStrTableName(String strTableName) {
		this.strTableName = strTableName;
	}

	public String getStrTableName2() {
		return strTableName2;
	}

	public void setStrTableName2(String strTableName2) {
		this.strTableName2 = strTableName2;
	}

	public String getStrId() {
		return strId;
	}

	public void setStrId(String strId) {
		this.strId = strId;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getFrequentCopyPoint() {
		return frequentCopyPoint;
	}

	public void setFrequentCopyPoint(String frequentCopyPoint) {
		this.frequentCopyPoint = frequentCopyPoint;
	}

	public String getStrCreator() {
		return strCreator;
	}

	public void setStrCreator(String strCreator) {
		this.strCreator = strCreator;
	}

	public String getDtCreatTime() {
		return dtCreatTime;
	}

	public void setDtCreatTime(String dtCreatTime) {
		this.dtCreatTime = dtCreatTime;
	}

	public String getDevice_id() {
		return device_id;
	}

	public void setDevice_id(String deviceId) {
		device_id = deviceId;
	}

	public String getChannel() {
		return channel;
	}

	public void setChannel(String channel) {
		this.channel = channel;
	}
    
    
}